<?php

require_once dirname(__FILE__).'/../lib/productenGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/productenGeneratorHelper.class.php';

/**
 * producten actions.
 *
 * @package    semramode
 * @subpackage producten
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productenActions extends autoProductenActions
{
    public function executeCreate(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->product = $this->form->getObject();
    
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

   public function executeUpdate(sfWebRequest $request)
  {
    $this->product = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->product);
        
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }


    protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())

    {
       
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      try {
         
        $product = $form->save();
     //  $form->saveEmbeddedForms();
   
      } catch (Doctrine_Validator_Exception $e) {

        $errorStack = $form->getObject()->getErrorStack();

        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors) {
            $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }if($form->getValue('filename') != ''){
 $product->createthumb(sfConfig::get('sf_upload_dir').'/images/'.$product->filename ,sfConfig::get('sf_upload_dir').'/images/tn_'.$product->getFilename(),182,213);
      }
      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $product)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

        $this->redirect('@product_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect(array('sf_route' => 'product_edit', 'sf_subject' => $product));
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }
  // apps/backend/modules/category/actions/actions.class.php
public function executeDeleteImage(sfWebRequest $request) {
  $image = Doctrine::getTable('image')->findOneBy('id', $request->getParameter('id'));
  $product = Doctrine::getTable('product')->find($image->getProduct()->getId());

  $image->delete();
$this->redirect(array('sf_route' => 'product_edit', 'sf_subject' => $product));
}


}
